<?php
/**
 * International Support
 * @author  Nam Nguyen <nam.ngvan@gmail.com>
 * @version 1.0
 * @since   Apr 17, 2013
 */
class Intl
{
	/**
	 * @static
	 * @access  public
	 * @var     Nuo_Intl
	 */
	static private $_instance = null;

	/**
	 * @static
	 * @access private
	 * @var    string
	 */
	static private $_locale = null;

	/**
	 * @static
	 * @access private
	 * @var    string
	 */
	static private $_language;

	/**
	 * @static
	 * @access private
	 * @return    void
	 */
	private function __construct()
	{
		$locale = '';

		if (isset($_COOKIE['locale']))
		{
			$locale = $_COOKIE['locale'];
		}

		if (!$locale)
		{
			$locale = Nuo::getConfig('core/site', 'locale');
		}

		self::$_locale = $locale;

		list($language, $ext) = explode('_', $locale, 2);

		self::$_language = $language;

		self::_initPhrase();
	}

	/**
	 * load phrase language
	 */
	static public function _initPhrase()
	{
		$lang = Intl::getLanguage();
	
		
		Nuo::getTable('core/phrase')->loadForTranslator($lang);

		self::$_vars = $data;

	}

	public static function getLanguage()
	{
		return self::$_language;
	}

	/**
	 * @param string $locale
	 * @return Intl_Locale
	 */
	public static function getLocale($locale = NULL)
	{
		static $locales = array();

		if (NULL == $locale)
		{
			$locale = self::$_locale;
		}

		$locale = strtolower(str_replace('-', '_', $locale));

		if (!isset($locales[$locale]))
		{
			require_once 'intl/classes//locale.php';
			$locales[$locale] = new Intl_Locale($locale ? $locale : self::getCurrentLocale());
		}

		return $locales[$locale];
	}

	public static function getMessage($lang)
	{
		static $messages = array();

		$lang = strtolower($lang);

		if (!isset($messages[$lang]))
		{
			require_once 'intl/classes//locale.php';
			$messages[$lang] = new Nuo_Intl_Message($lang ? $lang : self::getCurrentLanguage());
		}

		return $messages[$lang];
	}

	/**
	 * @return array IDs of the locales which the framework can recognize
	 */
	public static function getLocales()
	{
		static $locales;

		if ($locales === null)
		{
			$locales = array();
			$dataPath = dirname(__FILE__) . '/intl/ldml';
			$folder = @opendir($dataPath);
			while (($file = @readdir($folder)) !== false)
			{
				$fullPath = $dataPath . DIRECTORY_SEPARATOR . $file;
				if (substr($file, -4) === '.php' && is_file($fullPath))
					$locales[] = substr($file, 0, -4);
			}
			closedir($folder);
			sort($locales);
		}
		return $locales;
	}

	/**
	 * get locale string
	 * @return string
	 */
	static public function getCurrentLocale()
	{
		return self::$_locale;
	}

	/**
	 * get locale string
	 * @return string
	 */
	static public function getCurrentLanguage()
	{
		return self::$_language;
	}

	static public function getAllLanguages()
	{
		static $languages = null;

		if (null == $languages)
		{
			$cache = Nuo_Cache::instance();
			if (false == ($languages = $cache -> get('core/language/getAllLanuages')))
			{
				$languages = Model::getTable('core/language') -> getAllLanguages();
				$cache -> set('languages', $languages);
			}
		}

		return $languages;
	}

	static public function getAllCountries()
	{
		return Nuo::readConfigFile('country');
	}

	static public function getAllTimezones()
	{
		return Nuo::readConfigFile('timezone');
	}

	static public function getAllLocales()
	{
		return Nuo::readConfigFile('locale');
	}

	static public function toDuration($time)
	{
		$ret = array();

		// hours
		if ($time > 3600)
		{
			$ret[] = str_pad(floor($time / 3600), 2, "0", STR_PAD_LEFT);
			$time = $time % 3600;
		}

		// minutes
		$ret[] = str_pad(floor($time / 60), 2, "0", STR_PAD_LEFT);
		$time = $time % 60;

		// seconds
		$ret[] = str_pad(floor($time / 60), 2, "0", STR_PAD_LEFT);

		return implode(':', $ret);
	}

	static public function getTimeDiff($timestamp)
	{
		$diff = time() - $timestamp + 1;

		if ($diff < 60)
		{
			$delta = $diff;
			return "$diff seconds ago";
		}
		if ($diff < 3600)
		{
			$delta = floor($diff / 60);
			return "$delta minutes ago";
		}
		if ($diff < 86400)
		{
			$delta = floor($diff / 3600);
			return "$delta hours ago";
		}

		$delta = floor($diff / 86400);
		return "$delta days ago";

	}

	function __percentage()
	{
		return Intl::getLocale($locale) -> formatDateTime($value);
	}

	function __currency($value, $opt = NULL, $locale = NULL)
	{
		return Intl::getLocale($locale) -> formatCurrency($value);
	}

	/**
	 * @return string
	 * @param int $value
	 * @param string $type [full_date_time,short_date_time,full_date, short_date,
	 * full_time, short_time]
	 * @return string
	 */
	static public function formatDateTime($value, $date = 'full', $time = 'short')
	{
		return Intl::getLocale() -> formatDateTime($value, $date, $time);

	}

	static public function formatDate($value, $date = 'full')
	{
		return Intl::getLocale() -> formatDateTime($value, $date, null);

	}

	static function formatNumber($value, $opt = NULL, $locale = NULL)
	{
		return Intl::getLocale($locale) -> formatDecimal($value);
	}

	static public function getAllCurrencies()
	{
		static $currencies = null;

		if (null == $currencies)
		{
			$cache = Nuo_Cache::instance();
			if (false == ($currencies = $cache -> get('core/language/getAllCurrencies')))
			{
				$currencies = Model::getTable('core/currency') -> getAllCurrencies();
				$cache -> set('currencies', $currencies);
			}
		}

		return $currencies;
	}

	/**
	 * Translator
	 * @static
	 * @access   private
	 * @var      Translator
	 */
	static private $_translator = NULL;

	/**
	 * @static
	 * @access   private
	 * @var      array
	 */
	static private $_vars = array();

	/**
	 * @param string $name
	 * @param array $data
	 * @return string
	 */
	static public function __($msg, $data = array())
	{
		return strtr(isset(self::$_vars[$msg]) ? self::$_vars[$msg] : $msg, (array)$data);
	}

	/**
	 * @static
	 * @access  public
	 * @return  Nuo_Intl
	 */
	static function instance()
	{
		if (null == self::$_instance)
		{
			self::$_instance = new self;
		}
		return self::$_instance;
	}
}